<?php

/*
 * Methods calling remote functions
 *
 */

require_once('const.php');
require_once('xmlrpc.inc');

/**
 * Sends sms ( interface_protocol chapter 3.2 )
 * @param mobile      phone number
 * @param text        message text
 * @param id          id of corresponding incomming sms
 * @param type        0 - text, 1- binary
 */
function SendSMS($mobile, $text, $id, $type, $value, $partnerid, $origi)
{
	$method = "SendSMS";

	$args = array(
		new xmlrpcval($mobile, "string"),
		new xmlrpcval($text, "string"),
		new xmlrpcval($id, "int"),
		new xmlrpcval($type, "int"),
		new xmlrpcval($value, "int"),
		new xmlrpcval($partnerid, "int"),
		new xmlrpcval($origi, "string")
	);

	$res = callMethod($method, $args);

	return $res;
}

/**
 * Get new sms from the server ( interface_protocol chapter 3.3.1 )
 */
function GetNewSMS()
{
	$method = "GetNewSMS";

	$args = array();

	$res = callMethod($method, $args);

	return $res;
}

/**
 * Confirm retrival of sms  ( interface_protocol chapter 3.3.1 )
 * @param id             sms id
 */
function ConfirmNewSMS($id)
{
	$method = "ConfirmNewSMS";

	$args = array(new xmlrpcval($id, "int"));

	$res = callMethod($method, $args);

	return $res;
}

/**
 * Verify phone number format
 * @param mobile         phone number to verify
 */
function VerifyPhoneNumber($mobile)
{
	$method = "VerifyPhoneNumber";

	$args = array(new xmlrpcval($mobile, "string"));

	$res = callMethod($method, $args);

	return $res;
}


/**
 * Test server availability
 */
function TestXMLPRC()
{
	$method = "TestXMLRPC";

	$args = array();

	$res = callMethod($method, $args);

	return $res;
}


/**
 * Call method on remote server.
 *
 * @param method                method name
 * @param args                  arguments for method
 * @return result               xml-rpc result
 *
 */
function callMethod($method, &$args)
{
	global $host, $port, $secure, $uri;

	$user = false;
	$pass = false;

	$msg = new xmlrpcmsg($method, $args);

	$client = new xmlrpc_client($uri, $host, $port);

	$res = $client->send($msg);

	return $res;
}

/**
 * Checks the xmlrpc result
 * Simple error handling
 */
function checkResult($res)
{
	// check for low-level error
	if ($res == 0 || !is_a($res, 'xmlrpcresp'))
	{
		return array('code' => 1, 'message' => 'I/O Error');
	}
	else
	{
		if ($res->faultCode() == 0)
		{
			return array('code' => 0, 'message' => 'OK');
		}
		else
		{
			return array('code' => 2, 'message' => $res->faultString());
		}
	}
}


?>